﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmsection
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Dim DSWHIS As New DataSet
    Dim Primary_Key As Integer = -1

    Sub Load_Data()
        Try

            Strsql = "select section_id,section_name,note,active from section   order by active desc ,section_name"
            'Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Dim DT_Section As DataTable = _Mysql.GetMYSQLDataTable(Strsql, "section")
            'Pn_Framework.Set_GridSetting(Me.gridsection, DSWHIS.Tables("category"))
            With gridsection
                .DataSource = DT_Section
                '.Columns(0).HeaderText = "ลำดับ"
                '.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                '.Columns(1).HeaderText = "ชื่อประเภทรายการ"
                '.Columns(2).HeaderText = "หมายเหตุ"
                '.Columns(3).HeaderText = "เปิดใช้งาน"
            End With
        Catch ex As Exception
            '    Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_CategoryData"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub frmsection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
    End Sub
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        With frmNewSections
            ' .Section_ID = Me.CboSection.SelectedValue
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Load_Data()
            End If
        End With
    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        With frmEditSections
            If Primary_Key = -1 Then Exit Sub
            Strsql = "Select * from section where Section_id = " & Primary_Key
            Dim DT As DataTable
            DT = _Mysql.GetMYSQLDataTable(Strsql)
            If DT.Rows(0).Item("readonly") = True Then
                MsgBox("ขออภัยคุณไม่สามารถลบ section นี้ได้เนื่องจากเป็นค่าตั้งต้น", MsgBoxStyle.Exclamation, "ไม่สามารถทำรายการได้")
                Exit Sub
            End If

            ' .Section_ID = Me.CboSection.SelectedValue
            .Primary_Key = Primary_Key
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Load_Data()
            End If
        End With
    End Sub



    Private Sub BtnDelSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelSection.Click
        Try
            If Primary_Key = -1 Then Exit Sub
            Strsql = "Select * from section where Section_id = " & Primary_Key
            Dim DT As DataTable
            DT = _Mysql.GetMYSQLDataTable(Strsql)
            If DT.Rows(0).Item("readonly") = True Then
                MsgBox("ขออภัยคุณไม่สามารถลบ section นี้ได้เนื่องจากเป็นค่าตั้งต้น", MsgBoxStyle.Exclamation, "ไม่สามารถทำรายการได้")
                Exit Sub
            End If


            If MsgBox("คำเตือน การลบ section จะทำให้ category ที่มีอยู่ถูกลบไปด้วย กด yes เพื่อยืนยันกด No เพื่อยกเลิก", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "คุณต้องการลบข้อมูลหรือไม่") = MsgBoxResult.No Then Exit Sub


            Strsql = "Delete from Section where Section_id = " & Primary_Key
            'Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            _Mysql.MySQLExecute(Strsql)

            Strsql = "Delete from category where sectionid = " & Primary_Key
            'Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            _Mysql.MySQLExecute(Strsql)
            Load_Data()
            'Refresh Data
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelSection_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub gridsection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridsection.Click

    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try
            Dim Gv As GridView = sender
            Primary_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("section_id"))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
        Load_Data()
    End Sub


End Class